package com.ty.zls.listener;

import android.content.Context;
import android.os.Handler;
import android.text.InputType;
import android.widget.Toast;

import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.ty.zls.adapter.ArreageAdapter;
import com.ty.zls.bean.Account;
import com.ty.zls.bean.Constants;
import com.ty.zls.task.BaseTask;
import com.ty.zls.util.HttpUtil;
import com.ty.zls.util.StringUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ArreageItemListener implements ArreageAdapter.OnItemClickListener {


    private Context context;
    private ArreageAdapter adapter;
    private QMUITipDialog tipDialog;
    private Handler handler = new Handler();

    public ArreageItemListener(Context context, ArreageAdapter adapter){
        this.context = context;
        this.adapter = adapter;
    }

    @Override
    public void onItemClick(String operation, int position, List<Account> accounts) {
        if (Constants.PRE_SORT.equals(operation)) {
            // 置后操作
            Map<String,String> map = new HashMap<>();
            final Account account = accounts.get(position);
            if(account != null){
                map.put("accountId", account.getId());
                final String presort = Constants.PRE_SORT.equals(account.getPreSort())?"1":"0";
                map.put("presort",  presort);
                new BaseTask(HttpUtil.PRE_SORT, context, new BaseTask.SuccessCallback() {
                    @Override
                    public void excute(JSONObject result) {
                        //refreshListener.autoRefresh();
                        account.setPreSort(presort);
                        adapter.notifyDataSetChanged();
                        showTip();
                    }
                }).execute(map);
            }
        }else if(Constants.EDIT_SORT.equals(operation)){
            //修改排序号
            final Account account = accounts.get(position);
            final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(context);
            builder.setTitle("修改排序号")
                    .setPlaceholder("在此输入排序号")
                    .setDefaultText(account.getSort())
                    .setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
                    .addAction("取消", new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            dialog.dismiss();
                        }
                    })
                    .addAction("确定", new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(final QMUIDialog dialog, int index) {
                            final String text = builder.getEditText().getText().toString();
                            if(StringUtils.isEmpty(text)){
                                Toast.  makeText(context,"请输入排序号",Toast.LENGTH_SHORT).show();
                            }else{
                                if(text.equals(account.getSort())){
                                    dialog.dismiss();
                                    return;
                                }
                                Map<String,String> map = new HashMap<>();
                                map.put("sort",text);
                                map.put("accountId",account.getId());
                                new BaseTask(HttpUtil.EDIT_SORT, context, new BaseTask.SuccessCallback() {
                                    @Override
                                    public void excute(JSONObject result) {
                                        dialog.dismiss();
                                        account.setSort(text);
                                        adapter.notifyDataSetChanged();
                                        showTip();
                                    }
                                }).execute(map);
                            }
                        }
                    })
                    .create(com.qmuiteam.qmui.R.style.QMUI_Dialog).show();
        }
    }

    private void showTip(){
        tipDialog = new QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord("修改成功")
                .create();
        tipDialog.show();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                tipDialog.dismiss();
            }
        },1000);
    }


}
